<template>
  <div class="index">
    <head-text titleText="餐饮短信推送"></head-text>
    <div class="list-content">
      <div class="filter-area">
        <div class="operate-area">
          <el-button @click="add" plain type="success" size="mini"
            >添加推送</el-button
          >
        </div>
      </div>
      <div class="table-title">
        <span></span>
        <h5>短信推送记录列表</h5>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        size="mini"
        v-if="hackReset"
      >
        <el-table-column type="index" width="50" align="center">
        </el-table-column>
        <el-table-column
          prop="activityName"
          label="活动"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="activityTime"
          label="活动场次"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>

        <el-table-column prop="status" label="推送状态" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.status | sendFilter }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="sendTime"
          label="短信自动推送时间"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              {{ sendTimeFun(scope.row.sendTime) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="margin: 5px"
              plain
              size="mini"
              @click="view(scope.row)"
              >查看</el-button
            >
            <el-button
              type="danger"
              style="margin: 5px"
              plain
              size="mini"
              @click="edit(scope.row.smsConfigId)"
              v-if="scope.row.status == 0"
              >撤销</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <page
        :nowPage="nowPage"
        :total="total"
        :limit="limit"
        @pageChange="onPageChange"
        @sizeChange="onSizeChange"
      />
    </div>
    <dialog-com :dialogObj="dialogObj" @reflash="getList"></dialog-com>
  </div>
</template>

<script>
import page from "@c/page";
import headText from "@c/headText";
import dialogCom from "./dialogCom";
import { getsmsconfigListApi, updatesmsconfigApi } from "@s/api";
import { FLOOROBJ, SENDSTATUS } from "@u/wordbook";

export default {
  name: "index",
  data() {
    return {
      nowPage: 1,
      total: 0,
      limit: 10,
      hackReset: true,
      dialogObj: {
        title: "",
        show: false,
        form: {}
      },
      tableData: []
    };
  },
  components: {
    headText,
    dialogCom,
    page
  },
  mounted() {
    this.getList();
  },
  //过滤器
  filters: {
    floorFilter(val) {
      return FLOOROBJ[val];
    },
    sendFilter(val) {
      return SENDSTATUS[val];
    }
  },
  methods: {
    onPageChange(val) {
      this.nowPage = val;
      this.getList();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.getList();
    },
    sendTimeFun(val) {
      if (val) {
        return "活动前" + val + "小时";
      } else {
        return "立即推送";
      }
    },
    edit(smsConfigId) {
      this.$confirm("此操作将撤销该短信推送?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let json = {
          smsType: 1, // 1  餐饮
          smsConfigId: smsConfigId,
          status: 2 // 状态 2 已撤销
        };
        updatesmsconfigApi(json).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "撤销成功！"
          });
        });
      });
    },
    getList() {
      let json = {
        page: this.nowPage,
        limit: this.limit,
        smsType: 1 // 1  餐饮
      };
      getsmsconfigListApi(json).then(res => {
        this.hackReset = false;
        this.$nextTick(() => {
          this.tableData = res.list;
          this.total = res.total;
          this.hackReset = true;
        });
      });
    },
    add() {
      this.dialogObj.id = "";
      this.dialogObj.show = true;
      this.dialogObj.title = "推送消息";
    },
    view(val) {
      this.dialogObj.id = 1;
      this.dialogObj.form = val;
      this.dialogObj.show = true;
      this.dialogObj.title = "推送消息";
    }
  }
};
</script>
<style scoped lang="scss"></style>
